<?php namespace App\Controllers\Manager;

//http://www.codeforest.net/laravel4-simple-website-with-backend-1
 
use App\Models\News;
use App\Models\NewsImage;
use Input, Redirect, Sentry, Str, Response, Config;
use Notification;
 
class NewsController extends \BaseController {
 
    public function index()
    {
        return \View::make('manager.news.index')
            ->with('news', News::orderBy("date", "desc")->get());
    }
 
    public function show($id)
    {
        /*return \View::make('manager.pages.show')
            ->with('page', Page::find($id));*/
    }
 
    public function create()
    {
        return \View::make('manager.news.create');
    }
 
    public function store()
    {
        $news = new News;
	    list($d, $m, $y) = explode("/", Input::get("date"));
	    $news->date   = date_create("$y-$m-$d");
	    $news->title   = Input::get('title');
	    $news->slug    = Str::slug(Input::get('slug'));
	    $news->body   = Input::get('body');
	    $news->save();
        
        Notification::success('The news was saved.');
     
        return Redirect::route('manager.news.index');
    }
 
    public function edit($id)
    {
	    $news = News::find($id);
		
		return \View::make('manager.news.edit')
            ->with('news', $news);
    }
 
    public function update($id)
    {
        $news = News::find($id);
	    list($d, $m, $y) = explode("/", Input::get("date"));
	    $news->date   = date_create("$y-$m-$d");
	    $news->title   = Input::get('title');
	    $news->slug    = Str::slug(Input::get('slug'));
	    $news->body   = Input::get('body');

	    $news->save();
		
		Notification::success('The news was saved.');
     
        return Redirect::route('manager.news.edit', $news->id);
    }
 
    public function destroy($id)
    {
	    $news = News::find($id);
	    $news->delete();

        Notification::info('The news was deleted.');

        return Redirect::route('manager.news.index');
    }

    public function getSlug()
    {
        return Response::json(array(
            "val" => Str::slug(Input::get("str"))
        ));
    }
	
	public function uploadImage($id)
	{
		$news = News::find($id);
		
		if (Input::hasFile('theimage'))
		{
			$destinationPath = Config::get('app.newsimagepath').$id;
			$path_parts = pathinfo(Input::file('theimage')->getClientOriginalName());
			
			$newfilename = Str::slug($path_parts["filename"]).".".$path_parts["extension"];
			
			if(!file_exists($destinationPath."/".$newfilename))
			{
				Input::file('theimage')->move($destinationPath, $newfilename);
			
				$ni = new NewsImage;
				$ni->imagefilename = $newfilename;
				$ni->save();
				
				$news->images()->attach($ni->id);
			}
			
			return Response::json(array(
				"error" => 0 //Str::slug(Input::get("str"))
			));
		}
		else
		{
			return Response::json(array(
				"error" => "No image specified" //Str::slug(Input::get("str"))
			));
		}
	}
	
	public function getImages($newsid)
	{
		return \View::make('manager.news.images')
            ->with('news', News::find($newsid));
	}
	
	public function deleteImage($newsid, $imageid)
	{
		$news = News::find($newsid);
		$image = NewsImage::find($imageid);
		
		$news->images()->detach($image->id);
		if(file_exists(Config::get('app.newsimagepath').$newsid."/".$image->imagefilename))
		{
			unlink(Config::get('app.newsimagepath').$newsid."/".$image->imagefilename);
		}
		$image->delete();
	}
}